package com.interest.modules.githubwrapper.service.impl;

import com.ext.github.GitHubService;
import com.ext.github.pojo.Repo;
import com.interest.modules.githubwrapper.service.GithubWrapperService;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.util.List;

/**
 * Created by yangfeng on 16-12-19.
 */
@Service("githubWrapperService")
public class GithubWrapperServiceImpl implements GithubWrapperService {
    private static final String API_URL = "https://api.github.com/";

    @Override
    public List<Repo> repoList(String user) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        GitHubService service = retrofit.create(GitHubService.class);
        Call<List<Repo>> repos = service.listRepos(user);
        try {
            return repos.execute().body();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
